Questions/Réponses pour le calcul formel avec Sympy

Comment commencer une session de calcul formel?

On importe tout le contenu de sympy.

In [1]:
from sympy import *

Alternative
On aurait également pu utiliser la fonction init_session qui fournit des variables, des fonctions et l'affichage LaTeX.

Comment créer des expressions?

utilisation de symbols.

In [2]:
x,y=symbols('x y')
In [3]:
type(x)
Out[3]:
sympy.core.symbol.Symbol

On peut alors les utiliser pour créer des expressions plus complexes.

In [4]:
A=x**2+y**2
In [5]:
type(A)
Out[5]:
sympy.core.add.Add
In [6]:
B=x**3
type(B)
Out[6]:
sympy.core.power.Pow

Comment obtenir un bel affichage?

On notera les différences concernant l'affichage

In [7]:
init_printing()
In [8]:
A
Out[8]:
$$x^{2} + y^{2}$$
In [9]:
print(A)
x**2 + y**2
In [10]:
pprint(A)
 2    2
x  + y 
In [11]:
from IPython.display import display
In [12]:
display(A)
$$x^{2} + y^{2}$$

Comment créer des nombres rationnels?

In [13]:
demi=Rational(1,2)
display(demi)
$$\frac{1}{2}$$
In [14]:
display(1/2)
$$0.5$$

Comment convertir vers sympy

In [15]:
demiBis=sympify(1)/sympify(2)
demiBis
Out[15]:
$$\frac{1}{2}$$
In [16]:
S(1/2)
Out[16]:
$$0.5$$
In [17]:
S(1)/2
Out[17]:
$$\frac{1}{2}$$
In [18]:
S(8.2)
Out[18]:
$$8.2$$
In [19]:
type(S(8.2))
Out[19]:
sympy.core.numbers.Float
In [20]:
S("2*x**2+2*x+1")
Out[20]:
$$2 x^{2} + 2 x + 1$$

On peut créer des symboles à la volée

In [21]:
type(S("t"))
Out[21]:
sympy.core.symbol.Symbol

Comment utiliser l'infini?

In [22]:
oo>1
Out[22]:
$$\mathrm{True}$$
In [23]:
1/oo
Out[23]:
$$0$$

Comment dériver une expression

In [24]:
f=exp(x**2+x+1)*(log(1+x**4))
display(f)
$$e^{x^{2} + x + 1} \log{\left (x^{4} + 1 \right )}$$
In [25]:
f.diff(x)
Out[25]:
$$\frac{4 x^{3}}{x^{4} + 1} e^{x^{2} + x + 1} + \left(2 x + 1\right) e^{x^{2} + x + 1} \log{\left (x^{4} + 1 \right )}$$
In [26]:
diff(f,x)
Out[26]:
$$\frac{4 x^{3}}{x^{4} + 1} e^{x^{2} + x + 1} + \left(2 x + 1\right) e^{x^{2} + x + 1} \log{\left (x^{4} + 1 \right )}$$

Comment trouver une primitive?

In [27]:
g=sin(x+1)*cos(x)**2
g
Out[27]:
$$\sin{\left (x + 1 \right )} \cos^{2}{\left (x \right )}$$
In [28]:
integrate(g,x)
Out[28]:
$$- \frac{2}{3} \sin^{2}{\left (x \right )} \cos{\left (x + 1 \right )} + \frac{2}{3} \sin{\left (x \right )} \sin{\left (x + 1 \right )} \cos{\left (x \right )} - \frac{1}{3} \cos^{2}{\left (x \right )} \cos{\left (x + 1 \right )}$$

Comment calculer une intégrale?

In [29]:
integrate(g,(x,0,pi))
Out[29]:
$$\frac{2}{3} \cos{\left (1 \right )}$$
In [30]:
integrate(exp(-x**2),(x,-oo,+oo))
Out[30]:
$$\sqrt{\pi}$$

Comment calculer une limite?

In [31]:
f=(1+1/x)**x
display(f)
$$\left(1 + \frac{1}{x}\right)^{x}$$
In [32]:
f.limit(x,oo)
Out[32]:
$$e$$

Comment manipuler des $O$?

In [33]:
O(x+x**2)
Out[33]:
$$\mathcal{O}\left(x\right)$$

Comment développer une expression?

In [34]:
Ex=(x+1)**2+(x-1)**4
display(Ex)
$$\left(x - 1\right)^{4} + \left(x + 1\right)^{2}$$
In [35]:
Ex.expand()
Out[35]:
$$x^{4} - 4 x^{3} + 7 x^{2} - 2 x + 2$$
In [36]:
expand(Ex)
Out[36]:
$$x^{4} - 4 x^{3} + 7 x^{2} - 2 x + 2$$

Comment factoriser une expression?

In [37]:
P=x**4+5*x**3+5*x**2-5*x-6
P
Out[37]:
$$x^{4} + 5 x^{3} + 5 x^{2} - 5 x - 6$$
In [38]:
P.factor()
Out[38]:
$$\left(x - 1\right) \left(x + 1\right) \left(x + 2\right) \left(x + 3\right)$$
In [39]:
factor(P)
Out[39]:
$$\left(x - 1\right) \left(x + 1\right) \left(x + 2\right) \left(x + 3\right)$$

Comment simplifier une expression?

In [40]:
C=(x+1)/(x+2)+((x+2)*(x+3))/(x**3+8)
C
Out[40]:
$$\frac{x + 1}{x + 2} + \frac{\left(x + 2\right) \left(x + 3\right)}{x^{3} + 8}$$
In [41]:
C.simplify()
Out[41]:
$$\frac{x^{3} + 7 x + 10}{x^{3} + 8}$$

Comment simplifier une expression trigonométrique?

In [42]:
trigsimp(cos(x)**2+sin(x)**2)
Out[42]:
$$1$$

Comment obtenir une décomposition en éléments simples?

In [43]:
F=1/(x**4+5*x**3+5*x**2-5*x-6)
F
Out[43]:
$$\frac{1}{x^{4} + 5 x^{3} + 5 x^{2} - 5 x - 6}$$
In [44]:
apart(F)
Out[44]:
$$- \frac{1}{8 x + 24} + \frac{1}{3 x + 6} - \frac{1}{4 x + 4} + \frac{1}{24 x - 24}$$

Comment substituer une expression dans une variable

In [45]:
F=(x+2)*(x+3)
display(F)
X=x**2+x+1
display(X)
$$\left(x + 2\right) \left(x + 3\right)$$
$$x^{2} + x + 1$$
In [46]:
F.subs(x,X)
Out[46]:
$$\left(x^{2} + x + 3\right) \left(x^{2} + x + 4\right)$$

Pour plusieurs variables simultanément

In [47]:
Ff=(x+2)*(x*y+1)
display(Ff)
$$\left(x + 2\right) \left(x y + 1\right)$$
In [48]:
Ff.subs({x:y+x+1, y:x**2+y**2+1})
Out[48]:
$$\left(\left(x^{2} + y^{2} + 1\right) \left(x^{2} + x + y^{2} + 2\right) + 1\right) \left(x^{2} + x + y^{2} + 4\right)$$

Comment évaluer une expression?

In [49]:
g=sqrt(x**2+1)*log(x+2)
display(g)
g.subs(x,pi)
$$\sqrt{x^{2} + 1} \log{\left (x + 2 \right )}$$
Out[49]:
$$\sqrt{1 + \pi^{2}} \log{\left (2 + \pi \right )}$$
In [50]:
g.subs(x,pi).evalf()
Out[50]:
$$5.39823530532439$$
In [51]:
(pi+E).evalf()
Out[51]:
$$5.85987448204884$$
In [52]:
(pi+E).evalf(25)
Out[52]:
$$5.859874482048838473822931$$
In [53]:
N(pi+E)
Out[53]:
$$5.85987448204884$$
In [54]:
N(pi,50)
Out[54]:
$$3.1415926535897932384626433832795028841971693993751$$

Comment obtenir un développement en série entière?

In [55]:
ex=log(1+x)/(1-x)
display(ex)
$$\frac{\log{\left (x + 1 \right )}}{- x + 1}$$
In [56]:
ex.series()
Out[56]:
$$x + \frac{x^{2}}{2} + \frac{5 x^{3}}{6} + \frac{7 x^{4}}{12} + \frac{47 x^{5}}{60} + \mathcal{O}\left(x^{6}\right)$$
In [57]:
ex.series(n=3)
Out[57]:
$$x + \frac{x^{2}}{2} + \mathcal{O}\left(x^{3}\right)$$
In [58]:
ex.series(x0=2,n=5)
Out[58]:
$$- \log{\left (3 \right )} + \left(- \frac{1}{3} + \log{\left (3 \right )}\right) \left(x - 2\right) + \left(x - 2\right)^{2} \left(- \log{\left (3 \right )} + \frac{7}{18}\right) + \left(- \frac{65}{162} + \log{\left (3 \right )}\right) \left(x - 2\right)^{3} + \left(x - 2\right)^{4} \left(- \log{\left (3 \right )} + \frac{131}{324}\right) + \mathcal{O}\left(\left(x - 2\right)^{5}; x\rightarrow2\right)$$

Comment créer une équation?

In [59]:
equation=Eq(x+3,2)
equation
Out[59]:
$$x + 3 = 2$$

Comment résoudre une équation algébrique

In [60]:
equation=Eq(x+3,2)
In [61]:
solve(equation,x)
Out[61]:
$$\left [ -1\right ]$$
In [62]:
equation=Eq(x**2+3*x+1,2)
equation
Out[62]:
$$x^{2} + 3 x + 1 = 2$$
In [63]:
solutions=solve(equation,x)
solutions
Out[63]:
$$\left [ - \frac{3}{2} + \frac{\sqrt{13}}{2}, \quad - \frac{\sqrt{13}}{2} - \frac{3}{2}\right ]$$
In [64]:
type(solutions)
Out[64]:
list

Comment résoudre une équation de manière approchée

In [65]:
equation=Eq(cos(x),x)
equation
Out[65]:
$$\cos{\left (x \right )} = x$$
In [66]:
nsolve(equation,x,0)
Out[66]:
mpf('0.73908513321516064')
In [67]:
nsolve(Eq(x*exp(x),1),x,0)
Out[67]:
mpf('0.56714329040978387')

Comment résoudre une équation différentielle?

In [68]:
fonc=Function('f')
fonc
Out[68]:
f
In [69]:
equad=Eq(Derivative(fonc(x),x),9*fonc(x))
equad
Out[69]:
$$\frac{d}{d x} f{\left (x \right )} = 9 f{\left (x \right )}$$
In [70]:
dsolve(equad,fonc(x))
Out[70]:
$$f{\left (x \right )} = C_{1} e^{9 x}$$

Comment obtenir le code LaTeX d'une expression?

In [71]:
S("sqrt(x**2+1)").series(x,7)
Out[71]:
$$5 \sqrt{2} + \frac{7 \sqrt{2}}{10} \left(x - 7\right) + \frac{\sqrt{2}}{1000} \left(x - 7\right)^{2} - \frac{7 \sqrt{2}}{50000} \left(x - 7\right)^{3} + \frac{39 \sqrt{2}}{2000000} \left(x - 7\right)^{4} - \frac{1351 \sqrt{2}}{500000000} \left(x - 7\right)^{5} + \mathcal{O}\left(\left(x - 7\right)^{6}; x\rightarrow7\right)$$
In [72]:
latex(S("sqrt(x**2+1)").series(x,7))
Out[72]:
'5 \\sqrt{2} + \\frac{7 \\sqrt{2}}{10} \\left(x - 7\\right) + \\frac{\\sqrt{2}}{1000} \\left(x - 7\\right)^{2} - \\frac{7 \\sqrt{2}}{50000} \\left(x - 7\\right)^{3} + \\frac{39 \\sqrt{2}}{2000000} \\left(x - 7\\right)^{4} - \\frac{1351 \\sqrt{2}}{500000000} \\left(x - 7\\right)^{5} + \\mathcal{O}\\left(\\left(x - 7\\right)^{6}; x\\rightarrow7\\right)'

Comment définir une matrice?

In [74]:
M=Matrix([[1,0,1],[0,1,0],[1,0,1]])
M
Out[74]:
$$\left[\begin{matrix}1 & 0 & 1\\0 & 1 & 0\\1 & 0 & 1\end{matrix}\right]$$
In [75]:
M*M
Out[75]:
$$\left[\begin{matrix}2 & 0 & 2\\0 & 1 & 0\\2 & 0 & 2\end{matrix}\right]$$
In [76]:
M+M
Out[76]:
$$\left[\begin{matrix}2 & 0 & 2\\0 & 2 & 0\\2 & 0 & 2\end{matrix}\right]$$
In [77]:
N=Matrix([[x,x*y,x+y],[x*y,x+y,x],[x+y,x,x*y]])
N
Out[77]:
$$\left[\begin{matrix}x & x y & x + y\\x y & x + y & x\\x + y & x & x y\end{matrix}\right]$$
In [78]:
M*N
Out[78]:
$$\left[\begin{matrix}2 x + y & x y + x & x y + x + y\\x y & x + y & x\\2 x + y & x y + x & x y + x + y\end{matrix}\right]$$
In [82]:
Identity?
In [ ]: